package Presentacion.Principal;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;


public class MarketApplicationContext{

	private static ApplicationContext context;
	

//	public static String[] CONFIG_LOCATIONS = {
//		"classpath*:/applicationContext.xml",
//        };

	
	public static String[] CONFIG_LOCATIONS = {
//		"classpath*:/hibernate.cfg.xml",
//        "classpath*:/hibernate.reveng.xml",
		};


	
	public static synchronized ApplicationContext getContext(){
		
		if( context == null ){
			context = new ClassPathXmlApplicationContext(CONFIG_LOCATIONS);			
		}
		return context;		
	}
	
	public static synchronized Object getBean( String name ){
		
		return MarketApplicationContext.getContext().getBean(name);
		
	}
	
	public static void setUp() throws Exception {
        SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory");
        // obtengo la session correspondiente, allowCreate en true.
        Session session = SessionFactoryUtils.getSession(sessionFactory, true);
        // esto se hace para vincular una misma session a todo un mismo metodo
        // del junit
        TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
    }
}
